package com.springboot.demo.service;
import com.springboot.demo.entity.User;
import com.springboot.demo.mapper.LibrarianMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class LibrarianService {

    @Autowired
    private final LibrarianMapper librarianMapper;



    public LibrarianService(LibrarianMapper librarianMapper) {
        this.librarianMapper = librarianMapper;
    }

    /**
     * 登录
     * @param user 用户名和密码
     * @return Result
     */

    public User login(User user) {
        User result = new User();
        result.setSuccess(0);

        String userLevel= librarianMapper.login(user);
        String lname = librarianMapper.getName(user);
        try
        {
            if (userLevel == null){
                result.setMsg("用户名或密码不正确");
            }
            else if(userLevel.equals("librarian")) {
                result.setLname(lname);
                result.setMsg("登录成功！");
                result.setSuccess(1);
            }
            else if(userLevel.equals("admin")){
                result.setLname(lname);
                result.setMsg("登录成功！");
                result.setSuccess(2);
            }
            else result.setSuccess(3);
        } catch (Exception e){
            e.getStackTrace();
            result.setMsg(e.getMessage());

        }
        return result;
    }


}
